package com.example.project3.controller;

import com.example.project3.entity.Projects;
import com.example.project3.entity.User;
import com.example.project3.entity.WorkGroup;
import com.example.project3.exception.ServiceException;
import com.example.project3.service.impl.ProjectsServiceImpl;
import com.example.project3.service.impl.WorkGroupServiceImpl;
import com.example.project3.util.API;
import com.example.project3.util.MyJWT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName ProjectsController .java
 * @Description empty
 * @Author LiuJinyu
 * @Date 2023/5/31 15:12
 * @Version 1.0
 */
@RestController
@RequestMapping("/p")
public class ProjectsController {

    private ProjectsServiceImpl projectsServiceImpl;

    private MyJWT myJWT;
    private WorkGroupServiceImpl groupServiceImpl;


    @PostMapping("/new")
    public API createP(HttpServletRequest request, @RequestBody Projects project) {
        Integer uid = myJWT.getUid(request);
        project.setMasterId(uid);
        projectsServiceImpl.save(project);
        WorkGroup group = new WorkGroup();
        group.setProjectId(project.getProjectId());
        group.setUid(uid);
        groupServiceImpl.save(group);
        return API.success();
    }

    /**
     * 使用getMoreInfo获取信息，路径为/info/more
     *
     * @param projectId
     * @return
     */
    @Deprecated
    @GetMapping("/info")
    public API getInfo(@RequestParam Integer projectId) {
        return API.success(projectsServiceImpl.getById(projectId));
    }

    @GetMapping("/info/more")
    public API getMoreInfo(@RequestParam Integer projectId) {
        return API.success(projectsServiceImpl.getMoreInfo(projectId));
    }

    @PostMapping("/invite")
    public API invite(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        Integer uid = myJWT.getUid(request);
        User inviteUser;
        if (Integer.parseInt(map.get("masterId").toString()) != uid) {
            throw new ServiceException("非任务发布者");
        } else {
            Projects project = new Projects();
            project.setProjectId(Integer.parseInt(map.get("masterId").toString()));
            project.setProjectId(Integer.parseInt(map.get("projectId").toString()));
            inviteUser = new User();
            inviteUser.setEmail(map.get("email").toString());
            inviteUser = projectsServiceImpl.invite(project, inviteUser);
        }
        return API.success(inviteUser);
    }

    @GetMapping("/my")
    public API getMy(HttpServletRequest request) {
        Integer uid = myJWT.getUid(request);
        User user = new User();
        user.setUid(uid);
        return API.success(projectsServiceImpl.getMyParticipatingProject(user));
    }

    @Autowired
    public void setProjectsServiceImpl(ProjectsServiceImpl projectsServiceImpl) {
        this.projectsServiceImpl = projectsServiceImpl;
    }

    @Autowired
    public void setMyJWT(MyJWT myJWT) {
        this.myJWT = myJWT;
    }

    @Autowired
    public void setWorkGroupServiceImpl(WorkGroupServiceImpl workGroupServiceImpl) {
        this.groupServiceImpl = workGroupServiceImpl;
    }
}
